<template>
  <div>
    <el-button @click="willAdd" type="primary" class="but">添加</el-button>
    <!-- 表格 -->
    <!-- 绑定自定义事件willUpdate -->
    <!-- arr传给list -->
    <list-vue @willUpdate="willUpdate" :arr="arr" @init="getlist"></list-vue>

    <!-- 分页 -->
    <!-- 分页组件    
    -->
    <el-pagination
      @current-change="changePage"
      background
      layout="prev,pager,next"
      :total="total"
      :page-size="size"
    ></el-pagination>
    <!-- 弹框 -->
    <!-- 传递info  传递arr-->
    <!-- 绑定自定义事件getlist -->
    <form-vue :info="info" :arr="arr" @init="getlist" ref="form"></form-vue>
  </div>
</template>
<script>
import listVue from "./components/list.vue";
import formVue from "./components/form.vue";
import { mapGetters, mapActions } from "vuex";
import { reqrolelist, requserlist, requsercount } from "../../http/api";
import { toggle } from "../../mixins/index";
export default {
  components: {
    listVue,
    formVue
  },
  mixins: [toggle],
  props: [],
  data() {
    return {
      arr: [],
      total: 0, //总条数
      size: 2, //一页的数量
      page: 1 //当前的页码
    };
  },
  computed: {
    ...mapGetters({})
  },
  mounted() {
    this.getlist();
  },
  methods: {
    ...mapActions({}),
    requstList() {
      requserlist({
        page: this.page,
        size: this.size
      }).then(res => {
        if (res.data.code == 200) {
          let arr = res.data.list ? res.data.list : [];
          //如果当前页不是1页，且取到了0个数据，那么应该请求上一页;
          if(this.page != 1 && arr.length ==0){
           this.page--;
           this.requstList();
           return;    
          }
          this.arr = arr
        }
      });
    },
    //请求列表
    getlist() {
      this.requstList();
      //请求总数
      requsercount().then(res => {
        if (res.data.code == 200) {
          this.total = res.data.list[0].total;
          console.log(this.total);
        }
      });
    },
    //页码发生了改变
    changePage(e) {
      this.page = e;
      this.requstList();
    }
  }
};
</script>
<style scoped lang="less">
@import "../../less/index.less";
.but {
  margin: @margin10;
}
</style>